import 'package:effective/model/result_entity.dart';
import 'package:effective/model/speech_entity.dart';
import 'package:effective/service/api.dart';
import 'package:effective/utils/base_model.dart';
import 'package:effective/view/upload.dart';
import 'package:effective/view_model/user_model.dart';
import 'package:effective/widget/common_dialog.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';

class UserPageViewModel extends BaseModel{

  List<SpeechVideoEntity> speechVideoEntityList = []..add(new SpeechVideoEntity());

  UserPageViewModel({API api}) : super(api: api);

  Future<void> login(BuildContext context) async {
    ResultEntity resultEntity = await CommonDialog().loginDialog(context);

    if (resultEntity != null) {
      context.read<UserModel>().update(resultEntity.data['username']);
      context.read<UserModel>().token = resultEntity.data['token'];
      notifyListeners();
      loadData(context);
    }
  }

  Future<void> logout(BuildContext context) {
    showDialog(
        context: (context),
        builder: (context) {
          return AlertDialog(
            content: Container(
              child: Text('确认要退出登录吗？'),
            ),
            actions: [
              TextButton(
                  onPressed: () async {
                    ResultEntity resultEntity =
                    await api.logout(context.read<UserModel>().token);
                    if (resultEntity.code == 200) {
                      context.read<UserModel>().update("");
                    } else {
                      Fluttertoast.showToast(msg: resultEntity.message);
                    }
                    Navigator.pop(context);
                  },
                  child: Text('确认')),
              TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text('取消')),
            ],
          );
        });
  }

  Future loadData(BuildContext context){

  }

  void uploadNewSpeech(BuildContext context){
    Navigator.push(context, MaterialPageRoute(builder: (context)=>Upload()));
  }

  /*void analytic(BuildContext context){
    //Navigator.push(context, MaterialPageRoute(builder: (context)=>Upload()));
  }*/
}